#pragma once
#include "mysql.hpp"
#include "chat_session_member.hxx"
#include "chat_session_member-odb.hxx"

namespace xchat
{


class ChatSessionMemberTable
{
public:
    using ptr = std::shared_ptr<ChatSessionMemberTable>;

    ChatSessionMemberTable(const std::shared_ptr<odb::core::database>& db) 
        : _db(db) {}

    //单个会话成员新增
    bool append(ChatSessionMember &csm)
    {
        try
        {
            //开启事务
            odb::transaction trans(_db->begin());
            _db->persist(csm);
            //提交事务
            trans.commit();
        }catch(std::exception &e){
            LOG_ERROR("新增cheat_session_member用户失败 {}-{} : {}", csm.session_id(), csm.user_id(), e.what());
            return false;
        }
        return true;
    }
    //批量新增
    bool append(std::vector<ChatSessionMember>& csm_lists)
    {
        std::string err_session_id;
        try
        {
            //开启事务
            odb::transaction trans(_db->begin());
            for(auto &csm : csm_lists)
            {
                err_session_id = csm.session_id();
                _db->persist(csm);
            }
            //提交事务
            trans.commit();
        }catch(std::exception &e){
            LOG_ERROR("新增cheat_session_member用户失败 {}-{} : {}", err_session_id, csm_lists.size(), e.what());
            return false;
        }
        return true;
    }
    //删除指定会话中的指定成员 -- ssid && uid
    bool remove(ChatSessionMember &csm) //删除前必须查询
    {
        try
        {
            odb::transaction trans(_db->begin());
            typedef odb::query<ChatSessionMember> query;
            typedef odb::result<ChatSessionMember> result;
            _db->erase_query<ChatSessionMember>(query::session_id == csm.session_id() && query::user_id == csm.user_id());

            trans.commit();
        }
        catch(std::exception &e){
            LOG_ERROR("删除单会话成员失败 {}-{} : {}", csm.session_id(), csm.user_id(), e.what());
            return false;
        }
        return true;
    }
    //删除会话的所有成员信息
    bool remove(const std::string& ssid)
    {
        try
        {
            odb::transaction trans(_db->begin());
            typedef odb::query<ChatSessionMember> query;
            typedef odb::result<ChatSessionMember> result;
            _db->erase_query<ChatSessionMember>(query::session_id == ssid);

            trans.commit();
        }
        catch(std::exception &e){
            LOG_ERROR("删除会话所有成员失败{} : {}",ssid , e.what());
            return false;
        }
        return true;
    }
    //通过会话id 获取所有成员
    std::vector<std::string> members(const std::string& ssid)
    {
        std::vector<std::string> res;
        try
        {
            //开启事务
            odb::transaction trans(_db->begin());
            typedef odb::query<ChatSessionMember> query;
            typedef odb::result<ChatSessionMember> result;
            //_db->query_one<User> 返回user*
            result r(_db->query<ChatSessionMember>(query::session_id == ssid));
            for (const auto &it : r)
            {
                res.push_back(it.user_id());
            }
            //提交事务
            trans.commit();

        }catch(std::exception &e){
            LOG_ERROR("通过ssid获取全部成员失败{}-{}", ssid, e.what());
        }
        return res;
    }
private:
    std::shared_ptr<odb::core::database> _db;
};

}